function varargout = iCreateBackground(varargin)
% ICREATEBACKGROUND M-file for iCreateBackground.fig
%      ICREATEBACKGROUND, by itself, creates a new ICREATEBACKGROUND or raises the existing
%      singleton*.
%
%      H = ICREATEBACKGROUND returns the handle to a new ICREATEBACKGROUND or the handle to
%      the existing singleton*.
%
%      ICREATEBACKGROUND('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ICREATEBACKGROUND.M with the given input arguments.
%
%      ICREATEBACKGROUND('Property','Value',...) creates a new ICREATEBACKGROUND or raises the
%      existing singleton*.  Starting from the left, property value pairs
%      are
%      applied to the GUI before iCreateBackground_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to iCreateBackground_OpeningFcn via
%      varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help iCreateBackground

% Last Modified by GUIDE v2.5 23-Jan-2012 14:11:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @iCreateBackground_OpeningFcn, ...
                   'gui_OutputFcn',  @iCreateBackground_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT






%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Data handling related functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function OnOpen(handles)

global img;
global bClick;
global imgx;
global imgy;
global startx;
global starty;
global bObject;

global ox1s;
global oy1s;
global ox2s;
global oy2s;

global ncurrentframe;
global ncurrentindex;
global nobjs; 
global ncurrentobject;
global totalframes;
global frames;
global nframes;
global nframesedited;


global fvideo;
global v;

global savefile;

%assigned when loading savefile
global calibsubtractfilefrontfull;
global calibsubtractfilesidefull; 
global backgroundfilefull;
global bFrontView;

calibsubtractfilefrontfull = '';
calibsubtractfilesidefull = '';
bFrontView = -1;

savefile = 'videoprocessdata.mat';

load(savefile);


if bFrontView == 1
    set(handles.radiobuttonfront, 'Value', 1);
    set(handles.radiobuttonside, 'Value', 0);
elseif bFrontView == 0
    set(handles.radiobuttonfront, 'Value', 0);
    set(handles.radiobuttonside, 'Value', 1);
end

bClick = 0;
bObject = 0;
startx = 1;
starty = 1;
ncurrentobject = 0;


ox1s = [];
oy1s = [];
ox2s = [];
oy2s = [];    
    
nobjs = 0;
    
frames = {};
nframes = [];
nframesedited = 0;
ncurrentindex = 0;
ncurrentframe = 1;

if isempty(inputfilefull)
    errordlg('The inputfilefull variable in videoprocessdata.mat is empty. Please check that an input file was selected before opening this dialog.');
    return
end


if isempty(backgroundfilefull)
    len = length(backgroundfilefull);
    backgroundfilefull = strcat(inputfilefull(1:len-4), '_background.mat');
    
    save(savefile, 'inputfile', 'outputfile', 'inputfilefull', 'outputfilefull', ...
    'frontviewfile', 'sideviewfile', 'frontviewfilefull', 'sideviewfilefull',...
    'finaloutputfile', 'finaloutputfilefull', 'backgroundfilefull', 'calibsubtractfilefront',... 
    'calibsubtractfileside', 'calib2d3dfile', 'calibsubtractfilefrontfull', 'calibsubtractfilesidefull',... 
    'calib2d3dfilefull');
end

fvideo = inputfilefull;
datafile = backgroundfilefull;
v = mmreader(fvideo);


totalframes = v.NumberOfFrames;

GotoFrame(handles)

set(handles.editstartframe, 'String', num2str(1));
set(handles.editendframe, 'String', num2str(totalframes-1));
set(handles.editminstepsize, 'String', 50);


if exist(datafile, 'file')
    load(datafile);
    
    if ~isempty(nframes) > 0
        ncurrentindex = 1;            
    
        LoadArrays();      
             
        ncurrentframe = nframes(ncurrentindex);
        
        UpdateListBoxObjects(handles)
        UpdateListBoxFrames(handles)
        UpdateFrameNumber(handles, ncurrentframe);
        DrawObjects();    
    end
else
    set(handles.listboxframes, 'String', '');
    set(handles.listboxobjects, 'String', '');        
end

set(handles.texttotalframes, 'String', num2str(totalframes));

UpdateFrame(handles)

sz = size(img);
imgx = sz(2);
imgy = sz(1);








function CalcAuto(handles)

global calibsubtractfilefrontfull;
global calibsubtractfilesidefull;
global backgroundfilefull;
global fvideo;

global savefile;

nstartframe = str2num(get(handles.editstartframe, 'String'));
nendframe = str2num(get(handles.editendframe, 'String'));
nminstepsize = str2num(get(handles.editminstepsize, 'String'));

bf = get(handles.radiobuttonfront, 'Value');
bs = get(handles.radiobuttonside, 'Value');

if bf == 0 & bs == 0
    errordlg('Please set the view to front or side');
    return
end

if isempty(calibsubtractfilefrontfull) & isempty(calibsubtractfilesidefull)   
    load(savefile);
end


if bf == 1
    subtractfile = calibsubtractfilefrontfull;
else
    subtractfile = calibsubtractfilesidefull;
end




if isempty(subtractfile)
    errordlg('No subtraction file has been specified. Please close this window, select a subtraction file from the main window, and open this window again.');
    return
end

set(handles.textoutput, 'String', 'Calculating background, please wait...this may take at least a few minutes.');

[backgroundimg, bprocess] = CreateBackgroundImageAuto(nstartframe, nendframe, nminstepsize, fvideo, subtractfile, handles);


if bprocess == 1
    figure, imshow(backgroundimg)
    errordlg('There was a problem calculating all the background pixels. Please select a different frame range (most likely cause of issue), step size, or try a manual calculation.')
    set(handles.textoutput, 'String', '');
else
    save(backgroundfilefull, 'backgroundimg');
    set(handles.textoutput, 'String', 'Background has been calculated, background been sucessfully saved to file.');
    
    figure, image(backgroundimg)
end






function CalcManual(handles)


global backgroundfilefull;

global nframes;
global frames;
global v;

sz = size(frames);
len = sz(1);

if len <= 0
    return
end

img = read(v,1);
imgsz = size(img);

xlen = imgsz(1);
ylen = imgsz(2);

imbuf = zeros(xlen,ylen,3);

countbuf = zeros(xlen,ylen);


for i = 1:len

    nframe = nframes(i);
    
    img = read(v,nframe);
    dimg = double(img);
    
    countbuf = countbuf + 1;
    
    x1s = cell2mat(frames(i,1));
    x2s = cell2mat(frames(i,2));
    y1s = cell2mat(frames(i,3));
    y2s = cell2mat(frames(i,4));
    nobjs = cell2mat(frames(i, 5));

    
    for j = 1:nobjs        
        x1 = x1s(j);
        x2 = x2s(j);
        y1 = y1s(j);
        y2 = y2s(j);
        
        dimg(y1:y2, x1:x2, :) = 0;
        
        countbuf(y1:y2, x1:x2) = countbuf(y1:y2, x1:x2)-1;
    end
        
    for k = 1:xlen
        for l = 1:ylen
            imbuf(k,l,1) = imbuf(k,l,1) + dimg(k,l,1);
            imbuf(k,l,2) = imbuf(k,l,2) + dimg(k,l,2);
            imbuf(k,l,3) = imbuf(k,l,3) + dimg(k,l,3);
        end
    end
end

%{
a = find(countbuf(:) == 0);
a
if ~isempty(a)
    %return
end
%}


imbuf(:,:,1) = imbuf(:,:,1)./countbuf;
imbuf(:,:,2) = imbuf(:,:,2)./countbuf;
imbuf(:,:,3) = imbuf(:,:,3)./countbuf;

backgroundimg = uint8(imbuf);

save(backgroundfilefull, 'backgroundimg');
set(handles.textoutput, 'String', 'Background has been calculated, background been sucessfully saved to file.');
    
figure, image(backgroundimg)
    

function SaveData(handles)
global frames;
global nframes;
global ncurrentindex;
global ncurrentframe;
global ncurrentobject;

global datafile;

save(datafile, 'frames', 'nframes', 'ncurrentindex', 'ncurrentobject', 'ncurrentframe');




function AddObject(handles)

global x1;
global x2;
global y1;
global y2;

global ox1s;
global oy1s;
global ox2s;
global oy2s;

global nobjs; 


global ncurrentobject;

if x1 > 0 & x2 > 0 & y1 > 0 & y2 > 0     
       
      
    nobjs = nobjs + 1;    
    ncurrentobject = nobjs;    
      
    ox1s(nobjs) = x1;
    ox2s(nobjs) = x2;
    oy1s(nobjs) = y1;
    oy2s(nobjs) = y2;              
    
    UpdateListBoxObjects(handles);
    
    DrawObjects();       
    
    x1 = 0;
    x2 = 0;
    y1 = 0;
    y2 = 0;
    
    SaveFrame(handles)
end


function RemoveObject(handles)

global ox1s;
global oy1s;
global ox2s;
global oy2s;

global ncurrentindex;
global nobjs; 

global ncurrentobject;

if ncurrentindex <= 0
    return
end

if nobjs > 0 & ncurrentobject > 0 
    
    ox1s(ncurrentobject) = [];
    ox2s(ncurrentobject) = [];
    oy1s(ncurrentobject) = [];
    oy2s(ncurrentobject) = [];
  
    
    nobjs = nobjs - 1;
    
    if nobjs == 0
        %DisableRadioButtons(handles)
    else
        if ncurrentobject > nobjs
            ncurrentobject = ncurrentobject - 1;
        end            
    end
    
    UpdateListBoxObjects(handles)
    DrawObjects()        
    SaveFrame(handles)
end


function UpdateObjects(handles)


global ncurrentobject;

nindex = get(handles.listboxobjects, 'Value');
if nindex > 0
    ncurrentobject = nindex;
end

UpdateListBoxObjects(handles)
DrawObjects()
SaveFrame(handles)



function RemoveFrame(handles)

global frames;
global nframes;
global ncurrentindex;
global ncurrentframe;
global ncurrentobject;

if ncurrentindex <= 0 
    str = 'Current frame ('; 
    str = strcat(str, num2str(ncurrentframe)); 
    str = strcat(str, ') ');
    str = strcat(str, ' data does not exist. Click under Frames to select a different frame.');
    
    bwarn = 1;
    
    UpdateTextEvent(handles, str, bwarn);
    
    return
end

frames(ncurrentindex,:) = [];
nframes(ncurrentindex) = [];
ResetArrays();

ncurrentindex = 0;
ncurrentobject = 0;


UpdateListBoxObjects(handles)
UpdateListBoxFrames(handles)
DrawObjects()        

    
    

function SaveFrame(handles)

global frames;

global ox1s;
global ox2s;
global oy1s;
global oy2s;

global ncurrentindex;
global ncurrentframe;
global nobjs; 

global nframes;

a = find(nframes == ncurrentframe, 1);

if isempty(a)
    
    len = length(nframes);    
    ncurrentindex = len + 1;
    nframes(ncurrentindex) = ncurrentframe;            
else
    ncurrentindex = a;            
end

frames(ncurrentindex,:) = {ox1s, ox2s, oy1s, oy2s, nobjs};

UpdateListBoxFrames(handles)




function ResetArrays()

global ox1s;
global ox2s;
global oy1s;
global oy2s;

global nobjs; 




ox1s = [];
ox2s = [];
oy1s = [];
oy2s = [];

nobjs = 0;




function LoadArrays()

global frames;

global ox1s;
global ox2s;
global oy1s;
global oy2s;

global nobjs; 

global ncurrentindex; 

if ncurrentindex > 0
    ox1s = cell2mat(frames(ncurrentindex, 1));
    ox2s = cell2mat(frames(ncurrentindex, 2));
    oy1s = cell2mat(frames(ncurrentindex, 3));
    oy2s = cell2mat(frames(ncurrentindex, 4));
    nobjs = cell2mat(frames(ncurrentindex, 5));
    
end





function DrawObjects()

global ox1s;
global oy1s;
global ox2s;
global oy2s;

global nobjs; 

global imgorg;
global img;

global ncurrentobject;

if nobjs > 0
    
    img = imgorg;
    
    for i = 1:nobjs
                
        x1 = ox1s(i);
        x2 = ox2s(i);
        y1 = oy1s(i);
        y2 = oy2s(i);                                
                

        if i == ncurrentobject
             r = 250;
             g = 80;
             b = 80;
        else
             r = 200;
             g = 50;
             b = 50;
        end
     
        
        img(y1, x1:x2, 1) = r;
        img(y1, x1:x2, 2) = g;
        img(y1, x1:x2, 3) = b;
        
        img(y2, x1:x2, 1) = r;
        img(y2, x1:x2, 2) = g;
        img(y2, x1:x2, 3) = b;
                
        img(y1:y2, x1, 1) = r;
        img(y1:y2, x1, 2) = g;
        img(y1:y2, x1, 3) = b;
        
        img(y1:y2, x2, 1) = r;
        img(y1:y2, x2, 2) = g;
        img(y1:y2, x2, 3) = b;      
        
    end          
else
    img = imgorg;    
end

imshow(img)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Interface change/update related functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%






function UpdateListBoxObjects(handles)
global ox1s;
global ox2s;
global oy1s;
global oy2s;
global ncurrentobject;
global nobjs;

if nobjs > 0
    for i = 1:nobjs
                           
        s1 = num2str(ox1s(i));
        s2 = num2str(ox2s(i));
        s3 = num2str(oy1s(i));
        s4 = num2str(oy2s(i));
                    
        str(i,:) = strcat('Pos', {'  (h'}, s1, '-', s2, {'  v'}, s3, '-', s4, ')');            
    end    
else
    str = '';
end

if ncurrentobject > nobjs
    ncurrentobject = nobjs;
end
    
set(handles.listboxobjects, 'String', str);

if ncurrentobject > 0 
    set(handles.listboxobjects, 'Value', ncurrentobject);
end


function UpdateListBoxFrames(handles)
global nframes;
global ncurrentindex;


set(handles.listboxframes, 'String', num2str(nframes'));

len = length(nframes); 

if ncurrentindex > len
    ncurrentindex = len;
end

if len > 0
    if ncurrentindex > 0
        set(handles.listboxframes, 'Value', ncurrentindex);
    else
        set(handles.listboxframes, 'Value', 1);
    end
else
    set(handles.listboxframes, 'Value', 0);
end



function UpdateFrame(handles)

global totalframes; 
global ncurrentframe;

set(handles.editframe, 'String', num2str(ncurrentframe));

d = ncurrentframe/totalframes;
set(handles.sliderframe, 'Value', d);



function GotoFrame(handles)

global ncurrentindex;
global ncurrentframe;
global ncurrentobject;

global nframes;
global img;
global imgorg;
global v;
global totalframes;

if ncurrentframe > totalframes
    ncurrentframe = totalframes;
end

if ncurrentframe <=0
    ncurrentframe = 1;
end

img = read(v,ncurrentframe);
imgorg = img;

a = find(nframes == ncurrentframe, 1);

if isempty(a)            
    ncurrentindex = 0;
    ResetArrays();
    imshow(img);   
    
    UpdateListBoxObjects(handles)
else
    ncurrentindex = a;   

    LoadArrays();    
    DrawObjects();
    
    UpdateListBoxObjects(handles);
    ncurrentobject = get(handles.listboxobjects, 'Value');   
end
    


function UpdateFrameNumber(handles, n)
global totalframes;

set(handles.editframe, 'String', num2str(n));

d = n/totalframes;
set(handles.sliderframe, 'Value', d);
    
GotoFrame(handles);





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Functions called by event handlers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function ButtonGotoFrame(handles)

global totalframes; 
global ncurrentframe;

scurrentframe = get(handles.editframe, 'String');
n = str2num(scurrentframe);

if n > 0 & n <= totalframes
    ncurrentframe = n;
    
    d = ncurrentframe/totalframes;
    set(handles.sliderframe, 'Value', d);
    
    GotoFrame(handles)
end



function SliderGotoFrame(handles)

global totalframes; 
global ncurrentframe;

n = get(handles.sliderframe, 'Value');
ncurrentframe = floor(totalframes*n)+1;

if ncurrentframe > totalframes
    ncurrentframe = totalframes;
end

set(handles.editframe, 'String', num2str(ncurrentframe));
GotoFrame(handles)



function ListBoxFrameClicked(handles)

global ncurrentframe;
global nframes;
global totalframes; 

n = get(handles.listboxframes, 'Value');

if n > 0
    s = get(handles.listboxframes, 'String');
       
    nframe = str2num(s(n,:));        
        
    a = find(nframes == nframe, 1);        
    
    if ~isempty(a)
        ncurrentframe = nframe;
                             
        set(handles.editframe, 'String', num2str(ncurrentframe));
                
        d = ncurrentframe/totalframes;
                
        set(handles.sliderframe, 'Value', d);            
    
        GotoFrame(handles)
    end
end


function ListBoxObjectsClicked(handles)

global ncurrentobject;

nindex = get(handles.listboxobjects, 'Value');

if nindex > 0
    ncurrentobject = nindex;
end

DrawObjects()



function MouseButtonDown(handles)
global bClick;
global img;
global imgorg;
global imgx;
global imgy;
global startx; 
global starty;
global bObject;

global x1; 
global x2; 
global y1; 
global y2; 


point = get(gca,'CurrentPoint');

x = point(1,1);
y = point(1,2);

if x > 0 & y > 0 & x <= imgx & y <= imgy
    
    x1 = 0;
    x2 = 0;
    y1 = 0;
    y2 = 0;

    bClick = 1;
    bObject = 0;
    img = imgorg;
    DrawObjects();
    
    imshow(img)
    
    startx = x;
    starty = y;
end
    


function MouseButtonUp(handles)
global bClick;
global startx; 
global starty;
global bObject;


bClick = 0;

startx = 1;
starty = 1;



function MouseMoved(handles)

global bClick;
global img;
global imgorg;
global startx; 
global starty;
global imgx;
global imgy;
global bObject;

global x1; 
global x2;
global y1; 
global y2;

if bClick == 1
    point = get(gca,'CurrentPoint');
    
    x = point(1,1);
    y = point(1,2);

    if x > 0 & y > 0 & x <= imgx & y <= imgy
        bClick = 1;
        bObject = 1;
        
        img = imgorg;
        DrawObjects();
        
        if x > startx
            x1 = startx;
            x2 = x;
        else
            x1 = x;
            x2 = startx;            
        end
        
        if y > starty
            y1 = starty;
            y2 = y;
        else
            y1 = y;
            y2 = starty; 
        end
        
        x1 = round(x1);
        x2 = round(x2);
        y1 = round(y1);
        y2 = round(y2);
        
        nval = 255;
        
        img(y1, x1:x2, :) = nval;
        img(y2, x1:x2, :) = nval;
        img(y1:y2, x1, :) = nval;
        img(y1:y2, x2, :) = nval;
        
        %{
        img(y1-1:y1+1, x1:x2, :) = nval;
        img(y2-1:y2+1, x1:x2, :) = nval;
        img(y1:y2, x1-1:x1+1, :) = nval;
        img(y1:y2, x2-1:x2+1, :) = nval;
        %}
        
        imshow(img)
    end
end











%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Event handlers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --- Executes just before iCreateBackground is made visible.
function iCreateBackground_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to iCreateBackground (see VARARGIN)

% Choose default command line output for iCreateBackground
handles.output = hObject;


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes iCreateBackground wait for user response (see UIRESUME)
% uiwait(handles.figure1);

OnOpen(handles)

setappdata(0,'HandleMainGUI',hObject);


% --- Outputs from this function are returned to the command line.
function varargout = iCreateBackground_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

MouseButtonDown(handles)



% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

MouseButtonUp(handles)


% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

MouseMoved(handles)



% --- Executes on selection change in listboxobjects.
function listboxobjects_Callback(hObject, eventdata, handles)
% hObject    handle to listboxobjects (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxobjects contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxobjects

ListBoxObjectsClicked(handles)



% --- Executes during object creation, after setting all properties.
function listboxobjects_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxobjects (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in listboxframes.
function listboxframes_Callback(hObject, eventdata, handles)
% hObject    handle to listboxframes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listboxframes contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxframes

ListBoxFrameClicked(handles)


% --- Executes during object creation, after setting all properties.
function listboxframes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxframes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in pushbuttonro.
function pushbuttonro_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonro (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

RemoveObject(handles);



% --- Executes on button press in pushbuttondf.
function pushbuttondf_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttondf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

RemoveFrame(handles)



% --- Executes on button press in pushbuttonupdate.
function pushbuttonupdate_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonupdate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateObjects(handles)


% --- Executes on slider movement.
function sliderframe_Callback(hObject, eventdata, handles)
% hObject    handle to sliderframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

SliderGotoFrame(handles)


% --- Executes during object creation, after setting all properties.
function sliderframe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sliderframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function editframe_Callback(hObject, eventdata, handles)
% hObject    handle to editframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editframe as text
%        str2double(get(hObject,'String')) returns contents of editframe as a double


% --- Executes during object creation, after setting all properties.
function editframe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttongo.
function pushbuttongo_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttongo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ButtonGotoFrame(handles)



function editstartframe_Callback(hObject, eventdata, handles)
% hObject    handle to editstartframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editstartframe as text
%        str2double(get(hObject,'String')) returns contents of editstartframe as a double


% --- Executes during object creation, after setting all properties.
function editstartframe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editstartframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editendframe_Callback(hObject, eventdata, handles)
% hObject    handle to editendframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editendframe as text
%        str2double(get(hObject,'String')) returns contents of editendframe as a double


% --- Executes during object creation, after setting all properties.
function editendframe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editendframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editminstepsize_Callback(hObject, eventdata, handles)
% hObject    handle to editminstepsize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editminstepsize as text
%        str2double(get(hObject,'String')) returns contents of editminstepsize as a double


% --- Executes during object creation, after setting all properties.
function editminstepsize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editminstepsize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonautocalc.
function pushbuttonautocalc_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonautocalc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

CalcAuto(handles);


% --- Executes on button press in pushbuttonmanualcalc.
function pushbuttonmanualcalc_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonmanualcalc (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

CalcManual(handles);


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over sliderframe.
function sliderframe_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to sliderframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbuttonsetstartframe.
function pushbuttonsetstartframe_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsetstartframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


sframe = get(handles.editframe, 'String');
set(handles.editstartframe, 'String', sframe);


% --- Executes on button press in pushbuttonsetendframe.
function pushbuttonsetendframe_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsetendframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

sframe = get(handles.editframe, 'String');
set(handles.editendframe, 'String', sframe);


% --- Executes on button press in pushbuttonadd.
function pushbuttonadd_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonadd (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

AddObject(handles)


% --- Executes on button press in pushbuttonsf.
function pushbuttonsf_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsf (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

SaveFrame(handles)
